package com.yao.spider.yiyao;

import com.yao.spider.core.util.MyBatiesUtils;
import com.yao.spider.yiyao.bean.DongFMBean;
import com.yao.spider.yiyao.bean.MedicineInfoStandard;
import com.yao.spider.yiyao.bean.YaoYuanWang;
import com.yao.spider.yiyao.dao.*;
import com.yao.spider.yiyao.util.ExcelUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class StandardMedicineMain {
    public static SqlSession sqlSession = MyBatiesUtils.getSqlSession();
    //国产药剂型更新
    public static void initJY() {
        List<MedicineInfoStandard> medicineInfoStandards = ExcelUtil.readMedicineInfoStandardExcel("D:\\kaifa\\医保\\国家药品编码本位码信息（国产）.xlsx");
        Map<String, MedicineInfoStandard> medicineInfoStandardMap = new HashMap<String, MedicineInfoStandard>();
        for (MedicineInfoStandard standard : medicineInfoStandards) {
            medicineInfoStandardMap.put(standard.getApplyNo(), standard);
        }
        MedicineInfoStandardDao medicineInfoStandardDao = new MedicineInfoStandardDaoImpl();
        List<MedicineInfoStandard> medicineInfoStandardList = medicineInfoStandardDao.selectAll(sqlSession);
        for (MedicineInfoStandard standard : medicineInfoStandardList) {
            MedicineInfoStandard excelSt = medicineInfoStandardMap.get(standard.getApplyNo());
            if (excelSt != null) {
                standard.setMedicineJy(excelSt.getMedicineJy());
                medicineInfoStandardDao.updateJyById(sqlSession, standard);
            }
        }

    }
        //国产药
    public static void init() {

        List<MedicineInfoStandard> medicineInfoStandards = ExcelUtil.readMedicineInfoStandardExcel("D:\\kaifa\\医保\\国家药品编码本位码信息（国产）.xlsx");
        Map<String, MedicineInfoStandard> medicineInfoStandardMap = new HashMap<String, MedicineInfoStandard>();
        for (MedicineInfoStandard standard : medicineInfoStandards) {
            medicineInfoStandardMap.put(standard.getApplyNo(), standard);
        }
        MedicineInfoStandardDao fastMailDao = new MedicineInfoStandardDaoImpl();

        for (MedicineInfoStandard standard : medicineInfoStandards) {
            standard.setSourceType(1);
            fastMailDao.insert(standard, sqlSession);
        }
    }
    //进口药
    public static void initJk() {
        List<MedicineInfoStandard> medicineInfoStandards = ExcelUtil.readMedicineInfoStandardExcel("D:\\kaifa\\医保\\国家药品编码本位码信息（国产）.xlsx");
        MedicineInfoStandardDao fastMailDao = new MedicineInfoStandardDaoImpl();
//        SqlSession sqlSession = MyBatiesUtils.getSqlSession();
        for (MedicineInfoStandard standard : medicineInfoStandards) {
            standard.setSourceType(2);
            fastMailDao.insert(standard, sqlSession);
        }
    }

    public static void updateExp() {
//        SqlSession sqlSession = MyBatiesUtils.getSqlSession();
        MedicineInfoStandardDao medicineInfoStandardDao = new MedicineInfoStandardDaoImpl();
        DongFMBeanDao dongFMBeanDao = new DongFMBeanDaoImpl();
        YaoYuanWangDao yaoYuanWangDao = new YaoYuanWangDaoImpl();
        List<MedicineInfoStandard> medicineInfoStandards = medicineInfoStandardDao.selectShiYzNullAll(sqlSession);
        for (MedicineInfoStandard standard : medicineInfoStandards) {

            DongFMBean dongFMBean = dongFMBeanDao.selectByApplyNo(standard.getApplyNo(), sqlSession);
            if (dongFMBean != null && dongFMBean.getShiYz() != null) {
                standard.setAboutJb(dongFMBean.getAboutJb());
                standard.setShiYz(dongFMBean.getShiYz());
                medicineInfoStandardDao.updateById(sqlSession, standard);
            } else {
                YaoYuanWang yaoYuanWang = yaoYuanWangDao.selectByApplyNo(sqlSession, standard.getApplyNo());
                if (yaoYuanWang != null && yaoYuanWang.getShiYz() != null) {
                    standard.setShiYz(yaoYuanWang.getShiYz());
                    medicineInfoStandardDao.updateById(sqlSession, standard);
                } else {
                    yaoYuanWang = yaoYuanWangDao.selectByName(sqlSession, standard.getMedicineName());
                    standard.setShiYz(yaoYuanWang.getShiYz());
                    medicineInfoStandardDao.updateById(sqlSession, standard);
                }
            }
            System.out.println(standard.toString());
        }
    }
    public static void main(String[] args) {
//        init();
//        updateExp();
        initJY();
    }

}
